package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.file.service.FileStorageService;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmMaterialDto;
import com.heima.model.wemedia.pojos.WmMaterial;
import com.heima.utils.thread.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.service.WmMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;

/**
 * @author lzq
 * @data 2023/4/29 19:32
 */
@Service
@Transactional
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private FileStorageService fileStorageService;
    //图片上传
    @Override
    public ResponseResult upload(MultipartFile multipartFile) {
        //1检查参数
        if (multipartFile==null||multipartFile.getSize()==0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2上传图片
        //获取原图片名称
        String filename = multipartFile.getOriginalFilename();
        //截取原图片后缀
        String substring = filename.substring(filename.lastIndexOf("."));
        //设置图片唯一名称
        String uuid = UUID.randomUUID().toString().replace("-", "");
        //上传
        String imagePath=null;
        try {
             imagePath = fileStorageService.uploadImgFile("", uuid + substring, multipartFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        //保存到数据库
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setUserId(WmThreadLocalUtils.getUser().getId());
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short)0);
        wmMaterial.setUrl(imagePath);
        wmMaterial.setType((short)0);
        save(wmMaterial);
        return ResponseResult.okResult(wmMaterial);
    }

    //查看所有素材
    @Override
    public ResponseResult pageList(WmMaterialDto dto) {
        //1检测参数
        dto.checkParam();
        //查询
        //分页构造器
        Page page = new Page(dto.getPage(),dto.getSize());
        //查询构造器
        LambdaQueryWrapper<WmMaterial> lqw = new LambdaQueryWrapper<>();
        //判断是否是收藏查询
        if (dto.getIsCollection()!=null&&dto.getIsCollection()==1){
            lqw.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        //通过当前用户id查询
        lqw.eq(WmMaterial::getUserId,WmThreadLocalUtils.getUser().getId());
        //按照上传时间降序
        lqw.orderByDesc(WmMaterial::getCreatedTime);
        page(page,lqw);
        //返回数据
        ResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }
}
